package com.jingdianjichi.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthRoleDTOConvert;
import com.jingdianjichi.auth.application.dto.AuthRoleDTO;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthRoleBO;
import com.jingdianjichi.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;


@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Resource
    private AuthRoleDomainService authRoleDomainService;
    /**
     * 新增角色
     * @return
     */
    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("RoleController.add.authRoleDTO:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()),"角色key不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()),"角色名称不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTANCE
                    .convertDTOToBO(authRoleDTO);
            return Result.ok(authRoleDomainService.add(authRoleBO));

        }catch (Exception e){
            log.error("RoleController.add.error:{}",e.getMessage(),e);
            return Result.fail("新增角色失败");
        }
    }

    /**
     * 修改角色
     * @param authRoleDTO
     * @return
     */
    @RequestMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("RoleController.update.authRole:{}",authRoleDTO);
            }
            Preconditions.checkNotNull(authRoleDTO.getId(),"角色id不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()),"角色key不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()),"角色名称不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTANCE
                    .convertDTOToBO(authRoleDTO);
            return Result.ok(authRoleDomainService.update(authRoleBO));
        }catch (Exception e){
            log.error("RoleController.update.error:{}",e.getMessage(),e);
            return Result.fail("角色更新失败");
        }
    }
    /**
     * 删除角色
     */
    @RequestMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("RoleController.delete.authRole:{}",authRoleDTO);
            }
            Preconditions.checkNotNull(authRoleDTO.getId(),"角色id不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTANCE
                    .convertDTOToBO(authRoleDTO);
            return Result.ok(authRoleDomainService.delete(authRoleBO));
        }catch (Exception e){
            log.error("RoleController.delete.error:{}",e.getMessage(),e);
            return Result.fail("角色删除失败");
        }
    }

}
